﻿namespace WMS6.Web.CrossDock
{
    using System;
    using System.Collections.Generic;
    using System.Web.UI;
    using System.Web.UI.HtmlControls;
    using System.Web.UI.WebControls;
    using WMS6.Web;
    using WMS6.Web.CrossdockServiceRef;

    public class FlowThruAllocation1 : BasePage
    {
        protected HtmlInputButton btnAction;
        protected Button btnAllocate;
        protected Button btnCopy;
        protected Button btnDelete;
        protected Button btnGoBack;
        protected Button btnNew;
        protected Button btnSave;
        protected Button btnShipping;
        protected CheckBox chkApportion;
        protected CheckBox chkIsActive;
        protected DropDownList ddlStatus;
        protected HtmlForm F79571;
        protected GridView gvPickDetail;
        protected GridView gvXorderDetail;
        protected HtmlInputHidden hdnFlag;
        protected HtmlInputHidden hdnFrom;
        protected HtmlInputHidden hdnLineID;
        protected HtmlInputHidden hdnOrderID;
        protected HtmlInputHidden hdnType;
        protected HtmlInputButton htmlbtnReport;
        protected Label lblConsigneeID;
        protected Label lblDeliveryDate;
        protected Label lblDoor;
        protected Label lblExternalLoadID;
        protected Label lblExternOrderID;
        protected Label lblLoadID;
        protected Label lblMsg;
        protected Label lblOwner;
        protected Label lblOwnerID;
        protected Label lblPoID;
        protected Label lblReceiptID;
        protected Label lblRoute;
        protected Label lblStatus;
        protected Label lblStop;
        protected HtmlLink mycss;
        protected Label one1;
        protected Label one2;
        protected Label one3;
        protected TextBox txtConsigneeID;
        protected TextBox txtDeliveryDate;
        protected TextBox txtDoor;
        protected TextBox txtExternalLoadID;
        protected TextBox txtExternOrderID;
        protected TextBox txtLoadID;
        protected TextBox txtOwnerID;
        protected TextBox txtPoID;
        protected TextBox txtReceiptID;
        protected TextBox txtRoute;
        protected TextBox txtStop;

        protected void btnAddDetail_Click(object sender, EventArgs e)
        {
            base.Response.Redirect("FlowThruAllocationDetail.aspx?from=edit", false);
        }

        protected void btnAllocate_Click(object sender, EventArgs e)
        {
            List<string> lineID = base.GetSelectedIDsInGridView(this.gvXorderDetail, "LineID");
            List<string> Status = base.GetSelectedIDsInGridView(this.gvXorderDetail, "Status");
            if ((lineID == null) || (lineID.Count == 0))
            {
                this.lblMsg.Text = "请选择一项! ";
            }
            else
            {
                for (int i = 0; Status.Count > i; i++)
                {
                    if (Status[i].ToUpper() != "NEW")
                    {
                        this.lblMsg.Text = "状态不等于New无法分配!";
                        return;
                    }
                }
                string strTemp = string.Empty;
                try
                {
                    strTemp = CrossdockServiceClientProxy.GetInstance().XorderAllocate(this.txtReceiptID.Text, this.hdnOrderID.Value, lineID);
                    this.SetXordersInfo2Form();
                    this.lblMsg.Text = strTemp + "分配完成！";
                }
                catch (Exception ex)
                {
                    this.lblMsg.Text = "分配失败:" + ex.Message;
                }
                finally
                {
                    ScriptManager.RegisterStartupScript(this.Page, base.GetType(), "", "<script language='javascript'>setTab('one',2,3);</script>", false);
                }
            }
        }

        protected void btnCopy_Click(object sender, EventArgs e)
        {
            this.lblMsg.Text = "复制成功！";
        }

        protected void btnDelete_Click(object sender, EventArgs e)
        {
            this.lblMsg.Text = "删除成功！";
        }

        protected void btnDeleteDetail_Click(object sender, EventArgs e)
        {
            this.lblMsg.Text = "转化成功！";
        }

        protected void btnGoBack_Click(object sender, EventArgs e)
        {
            base.Response.Redirect("FlowThruAllocationList.aspx?from=edit", false);
        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            this.lblMsg.Text = "保存成功！";
        }

        protected void btnShipping_Click(object sender, EventArgs e)
        {
            List<string> pickDetailID = base.GetSelectedIDsInGridView(this.gvPickDetail, "PickDetailID");
            List<string> Status = base.GetSelectedIDsInGridView(this.gvPickDetail, "Status");
            if ((pickDetailID == null) || (pickDetailID.Count == 0))
            {
                this.lblMsg.Text = "请选择一项! ";
            }
            else
            {
                for (int i = 0; Status.Count > i; i++)
                {
                    if (Status[i].ToUpper() != "NEW")
                    {
                        this.lblMsg.Text = "状态不等于New无法发货!";
                        return;
                    }
                }
                string strTemp = string.Empty;
                try
                {
                    strTemp = CrossdockServiceClientProxy.GetInstance().XPickDetailShipping(pickDetailID);
                    this.SetXordersInfo2Form();
                    this.lblMsg.Text = strTemp + "发货完成！";
                }
                catch (Exception ex)
                {
                    this.lblMsg.Text = "发货失败:" + ex.Message;
                }
                finally
                {
                    ScriptManager.RegisterStartupScript(this.Page, base.GetType(), "", "<script language='javascript'>setTab('one',3,3);</script>", false);
                }
            }
        }

        protected string FormatFlowThroUrl(string url, string lineID)
        {
            return string.Format(url, this.hdnFrom.Value.Trim(), this.hdnOrderID.Value, lineID);
        }

        protected void gvPickDetail_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                int i = this.gvPickDetail.PageIndex * this.gvPickDetail.PageSize;
                e.Row.Cells[0].Text = ((e.Row.RowIndex + 1) + i).ToString();
            }
        }

        protected void gvXorderDetail_Init(object sender, EventArgs e)
        {
        }

        protected void gvXorderDetail_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                int i = this.gvXorderDetail.PageIndex * this.gvXorderDetail.PageSize;
                e.Row.Cells[2].Text = ((e.Row.RowIndex + 1) + i).ToString();
            }
        }

        protected void gvXorderDetail_Sorting(object sender, GridViewSortEventArgs e)
        {
        }

        private void LoadData()
        {
            base.BindData2DDLBySysCode(this.ddlStatus, "OrderStatus");
            if (!string.IsNullOrEmpty(base.Request.QueryString["ID"]))
            {
                this.hdnOrderID.Value = base.Request.QueryString["ID"];
                this.txtReceiptID.Text = base.Request.QueryString["ReceiptID"];
                this.SetXordersInfo2Form();
            }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!base.IsPostBack)
            {
                base.InitPage(this.Page);
                this.LoadData();
            }
        }

        private void SetXordersInfo2Form()
        {
            string orderID = this.hdnOrderID.Value;
            XordersInfo xOrderInfo = CrossdockServiceClientProxy.GetInstance().GetXordersInfoByID(orderID);
            base.DataEntityViewState = xOrderInfo;
            if (xOrderInfo != null)
            {
                this.ddlStatus.SelectedValue = xOrderInfo.Status;
                this.txtDoor.Text = xOrderInfo.Door;
                this.txtOwnerID.Text = xOrderInfo.OwnerID;
                this.txtConsigneeID.Text = xOrderInfo.ConsigneeID;
                this.txtRoute.Text = xOrderInfo.Route;
                this.txtDeliveryDate.Text = base.GetPreferenceShortDate(xOrderInfo.DeliveryDate);
                this.txtPoID.Text = xOrderInfo.PoID;
                this.txtExternOrderID.Text = xOrderInfo.ExternOrderID;
                this.txtStop.Text = xOrderInfo.Stop;
                this.txtExternalLoadID.Text = xOrderInfo.ExternalLoadID;
                this.txtLoadID.Text = xOrderInfo.LoadID;
                this.chkApportion.Checked = xOrderInfo.Apportion == "Y";
                this.chkIsActive.Checked = xOrderInfo.IsActive == "Y";
                base.BindData2GridView(this.gvXorderDetail, xOrderInfo.XOrderDetailInfos);
                base.BindData2GridView(this.gvPickDetail, xOrderInfo.XpickDetailInfos);
            }
        }
    }
}

